package com.lemeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lemeng.entity.Goods;
import com.lemeng.entity.PageResult;
import com.lemeng.entity.Result;
import com.lemeng.pojo.Sku;
import com.lemeng.pojo.Spu;
import com.lemeng.service.*;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("spu")
public class SpuController {

    @Reference
    SpuService spuService;


    @GetMapping("findAll")
    public List<Goods> findAll(){
        return spuService.findAll();
    }

    @GetMapping("findPage")
    public PageResult<Goods> findPage(Integer page, Integer size) {
        return spuService.findPage(page,size);
    }

    @PostMapping("findList")
    public List<Goods> findList(@RequestBody Map<String, Object> serchMap) {
        return spuService.findList(serchMap);
    }
    @PostMapping("findPage")
    public PageResult<Goods> findPage(Integer page, Integer size,@RequestBody Map<String, Object> serchMap) {
        return spuService.findPage(page,size,serchMap);
    }


    @Reference
    CategoryBrandService categoryBrandService;


    @Reference
    TemplateService templateService;
    @Reference
    SpecService specService;
    @Reference
    ParaService paraService;
    @Reference
    AlbumService albumService;

    //查询所有修改需要的数据
    @GetMapping("findSpuAndOtherData")
    public Map<String,Object> findSpuAndOtherData(@RequestParam String id) {
        HashMap<String, Object> hashMap = new HashMap<>();

        Goods goods = spuService.findById(id);
        hashMap.put("spuData",goods);

//        brandList:[],
        hashMap.put("brandList",categoryBrandService.findBrandByCategoryIdByMybatisXml(goods.getSpu().getCategory3Id()));
//        templateList:[],

        hashMap.put("templateList",templateService.findAll());
//        paraList:[],
        HashMap<String, Object> searchMap = new HashMap<>();
        searchMap.put("templateId",goods.getSpu().getTemplateId());
        hashMap.put("paraList",paraService.findList(searchMap));
//        specList:[],
        hashMap.put("specList",specService.findList(searchMap));
//        albumList:[],
        hashMap.put("albumList",albumService.findAll());

        return hashMap;
    }

    @PostMapping("add")
    public Result add(@RequestBody Goods goods) {
        spuService.add(goods);
        return new Result();
    }
    @PostMapping("update")
    public Result update(@RequestBody Goods goods) {
        spuService.update(goods);
        return new Result();
    }

    @GetMapping("delete")
    public Result delete(Integer id) {
        spuService.delete(id);
        return new Result();
    }
    @GetMapping("deleteAll")
    public Result delete(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            spuService.delete(Integer.parseInt(id));
        }
        return new Result();
    }

    @PostMapping("changeGoodsMarketable")
    public Result changeGoodsMarketable(@RequestBody Spu spu) {
        spuService.updateIsMarketable(spu);
        return new Result();
    }


    @PostMapping("editSkuInfo")
    public Result editSkuInfo(@RequestBody List<Sku> skuList) {
        spuService.editSkuInfo(skuList);
        return new Result();
    }

}
